package com.evan.webadmin.biz.controller;

import com.evan.common.core.annotation.MyRequestBody;
import com.evan.common.core.object.MyPageData;
import com.evan.common.core.object.MyPageParam;
import com.evan.common.core.object.ResponseResult;
import com.evan.common.core.util.MyPageUtil;
import com.evan.webadmin.biz.config.BizConfig;
import com.evan.webadmin.biz.model.ChallengeModule;
import com.evan.webadmin.biz.service.IChallengeModuleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 闯关模块表 前端控制器
 * </p>
 *
 * @author Pavel
 * @since 2025-03-06
 */
@RestController
@RequestMapping("/admin/biz/challenge/module")
public class ChallengeModuleController {

    @Autowired
    private IChallengeModuleService challengeModuleService;
    @Autowired
    BizConfig bizConfig;

    @PostMapping("/findByPage")
    public ResponseResult<MyPageData<ChallengeModule>> findByPage(@MyRequestBody MyPageParam pageParam){
        if (pageParam != null) {
            PageMethod.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        }
        List<ChallengeModule> list = challengeModuleService.list();
        list.forEach(item -> {
            item.setFullFilePath(bizConfig.getDownloadPrefix() + item.getImgUrl());
        });
        long totalCount = 0L;
        if (list instanceof Page) {
            totalCount = ((Page<ChallengeModule>) list).getTotal();
        }
        return ResponseResult.success(MyPageUtil.makeResponseData(list, totalCount));
    }

    @PostMapping("/list")
    public ResponseResult<List<ChallengeModule>> list() {
        return ResponseResult.success(challengeModuleService.list());
    }

    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody ChallengeModule challengeModule) {
        if(challengeModule.getId() == null){
            challengeModule.setCreateTime(new Date());
            challengeModuleService.save(challengeModule);
        }else{
            challengeModule.setUpdateTime(new Date());
            challengeModuleService.updateById(challengeModule);
        }
        return ResponseResult.success();
    }

    @PostMapping("/findById")
    public ResponseResult<ChallengeModule> findById(@MyRequestBody  Long id) {
        ChallengeModule module = challengeModuleService.getById(id);
        module.setFullFilePath(bizConfig.getDownloadPrefix() + module.getImgUrl());
        return ResponseResult.success(module);
    }

    @PostMapping("/findByFront")
    public ResponseResult<List<ChallengeModule>> findByFront() {
        List<ChallengeModule> list = challengeModuleService.findByFront();
        list.forEach(item -> {
            item.setFullFilePath(bizConfig.getDownloadPrefix() + item.getImgUrl());
        });
        return ResponseResult.success(list);
    }

    @PostMapping("/delete")
    public ResponseResult delete(@MyRequestBody Long id) {
        challengeModuleService.removeById(id);
        return ResponseResult.success();
    }
}

